<?php
namespace app;

use app\ApiException;
use app\Pdo\PdoSource;
use app\Middleware\valiToken;

class Bootstrap
{
	protected $controller;

	protected $controllerName;

	protected $method;

	/**
	 * [run 运行程序]
	 * @author LYL 2017-06-26
	 * @return [type] [description]
	 */
	public function run()
	{
		//throw new ApiException("Error Processing Request", 500);

		$this->toRout();

		$controller = 	$this->controller;

		$method 	=	$this->method;

		valiToken::token($this->controllerName);

		$result = $controller->$method();

		echo toJson($result);
	}

	/**
	 * [toRout 过滤路由参数]
	 * @author LYL 2017-06-26
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	private function toRout()
	{
		$uri = urldecode(
			parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
		);
		
		$request = explode('/', $uri);

		if(count($request) != 3){

			return errorCode(404);

		}

		$this->controllerName  = ucfirst($request[1]);

		$method=$request[2];

		$class = 'app\Controller\\'.$this->controllerName;

		$this->controller = new $class();

		$methodArray = array_flip(get_class_methods($this->controller));

		if(!isset($methodArray[$method])){
			return errorCode(404);
		}

		$this->method = $method;

	}
}